<template>
  <el-dialog title="编辑参数" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="editForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="属性" prop="field">
        <el-input v-model="form.field"></el-input>
      </el-form-item>
      <el-form-item label="值" prop="val">
        <el-input v-model="form.val"></el-input>
      </el-form-item>
      <el-form-item label="种类" prop="type">
        <el-radio v-model="form.type" label="云币">云币</el-radio>
        <el-radio v-model="form.type" label="余额">余额</el-radio>
      </el-form-item>
      <el-form-item label="类型" prop="send_type">
        <el-radio v-model="form.send_type" label="定额">定额</el-radio>
        <el-radio v-model="form.send_type" label="比例">比例</el-radio>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.note"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('editForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { baseUrl } from "@/config/env";
const urls = {
  save: baseUrl + "/zweixin/sys/set/edit",
  get: baseUrl + "/zweixin/sys/set/get"
};
export default {
  data() {
    return {
      dialogVisible: false,
      data: [],
      form: {
        kid: null,
        field: null,
        val: null,
        note: null,
        type: null,
        send_type: null
      },
      rules: {
        field: [{ required: true, message: "请输入属性", trigger: "blur" }],
        val: [{ required: true, message: "请输入属性值", trigger: "blur" }]
      }
    };
  },
  components: {},
  methods: {
    show(kid) {
      this.dialogVisible = true;
      if (this.$refs["editForm"]) {
        this.$refs["editForm"].resetFields();
      }
      this.fetch(kid);
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    },
    fetch(kid) {
      axios.get(urls.get, { params: { kid: kid } }).then(res => {
        let data = res.data.result;
        this.form = {
          kid: data.kid,
          field: data.field,
          val: data.val,
          note: data.note,
          type: data.type,
          send_type: data.send_type
        };
      });
    }
  }
};
</script>